import $C from './config.js'
import $H from './request.js';
export default {
    // 获取存储列表数据
    getStorage(key){
        let data = null;
		// #ifdef H5
		if($C.env === 'dev'){
		    data = window.sessionStorage.getItem(key)
		} else {
		    data = uni.getStorageSync(key)
		}
		// #endif
        // #ifndef H5
        data = uni.getStorageSync(key)
        // #endif
        return data
    },
    // 设置存储
    setStorage(key,data){
        // #ifdef H5
        if($C.env === 'dev'){
            return window.sessionStorage.setItem(key,data)
        } else {
            return uni.setStorageSync(key,data)
        }
        // #endif
		// #ifndef H5
		return uni.setStorageSync(key,data)
		// #endif
    },
    // 删除存储
    removeStorage(key){
        // #ifdef H5
        if($C.env === 'dev'){
            return window.sessionStorage.removeItem(key);
        } else {
            return uni.removeStorageSync(key)
        }
        // #endif
		// #ifndef H5
		return uni.removeStorageSync(key)
		// #endif
    },
	fenToYuan(price) {
		if (typeof price === 'undefined') return '0.00'
		  const parsedNumber = typeof price === 'string' ? parseFloat(price) : price
		  return (parsedNumber / 100.0).toFixed(2)
	},
	isValidPhoneNumber(phone) {
		if(phone == null || phone == undefined || phone == '') {
			return false;
		}
	    const regex = /^1\d{10}$/;
	    return regex.test(phone);
	},
	getAccessToken() {
		let token = 'Bearer ' + this.getStorage('token')
		return token;
	},
	isLogin() {
		let token = this.getStorage('token');
		if(token && token != '' && token != 'null' && token != 'NULL' && token != undefined) {
			return true;
		}
		return false;
	},
	getOpenId() {
		let userCache = this.getStorage('loginUser');
		if(userCache && userCache != '' && userCache != 'null' && userCache != undefined) {
			let user = JSON.parse(userCache);
			return user.openid;
		}
		return null;
	},
	async checkText(content) {
		let openId = this.getOpenId();
		let check = false;
		await $H.get('/infra/rule/checkText',{content: content, appId: 44, openId: openId}).then(res => {			
			check = res;
		})
		return check;
	},
	createUUID() {
	  var dt = new Date().getTime(); // 当前时间毫秒数
	  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
	      var r = (dt + Math.random()*16)%16 | 0;
	      dt = Math.floor(dt/16);
	      return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
	  });
	  return uuid;
	}
}